package com.tudouji.project.store.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.tudouji.common.exception.CustomException;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.product.domain.Salesspecification;
import com.tudouji.project.product.mapper.SalesspecificationMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.store.mapper.StockunitMapper;
import com.tudouji.project.store.domain.Stockunit;
import com.tudouji.project.store.service.IStockunitService;

/**
 * 库存单位Service业务层处理
 * 
 * @author tudouji
 * @date 2021-01-06
 */
@Service
@Slf4j
public class StockunitServiceImpl implements IStockunitService 
{
    @Autowired
    private StockunitMapper stockunitMapper;
    @Autowired
    private SalesspecificationMapper salesspecificationMapper;

    /**
     * 查询库存单位
     * 
     * @param id 库存单位ID
     * @return 库存单位
     */
    @Override
    public Stockunit selectStockunitById(Long id)
    {
        return stockunitMapper.selectStockunitById(id);
    }

    /**
     * 查询库存单位列表
     * 
     * @param stockunit 库存单位
     * @return 库存单位
     */
    @Override
    public List<Stockunit> selectStockunitList(Stockunit stockunit)
    {
        return stockunitMapper.selectStockunitList(stockunit);
    }

    /**
     * 新增库存单位
     * 
     * @param stockunit 库存单位
     * @return 结果
     */
    @Override
    public int insertStockunit(Stockunit stockunit)
    {
        return stockunitMapper.insertStockunit(stockunit);
    }

    /**
     * 修改库存单位
     * 
     * @param stockunit 库存单位
     * @return 结果
     */
    @Override
    public int updateStockunit(Stockunit stockunit)
    {
        return stockunitMapper.updateStockunit(stockunit);
    }

    /**
     * 批量删除库存单位
     * 
     * @param ids 需要删除的库存单位ID
     * @return 结果
     */
    @Override
    public int deleteStockunitByIds(Long[] ids)
    {
        return stockunitMapper.deleteStockunitByIds(ids);
    }

    /**
     * 删除库存单位信息
     * 
     * @param id 库存单位ID
     * @return 结果
     */
    @Override
    public int deleteStockunitById(Long id)
    {
        return stockunitMapper.deleteStockunitById(id);
    }

    @Override
    public AjaxResult getUnits(String speguid) {
        // 查询规格
        Salesspecification spe =salesspecificationMapper.selectSalesspecificationById(speguid);
        List<Stockunit> units=new ArrayList<>();
        Stockunit unit1 =stockunitMapper.selectStockunitById(spe.getStockunitid());
        unit1.setType(1);
        units.add(unit1);
        /*if(spe.getSfunitid()!=null&&unit1.getIsUnit()==1){
            Stockunit unit2=new Stockunit();
            unit2.setId(spe.getSfunitid());
            unit2=stockunitMapper.selectStockunitById(spe.getSfunitid());
            unit2.setType(2);
            units.add(unit2);
        }*/
        return AjaxResult.success(units);
    }

    /**
     * 两个重量单位之间的转换率
     * @param fromunitId
     * @param tounitId
     * @return
     */
    public BigDecimal getRate(Long fromunitId, Long tounitId) {
        if (fromunitId == tounitId){
            return BigDecimal.ONE;
        }
        Stockunit fromunit = selectStockunitById(fromunitId);
        if(fromunit==null){
            throw new CustomException("物料单位获取异常"+fromunitId);
        }
        if(fromunit.getIsUnit()!=1){
            log.info(fromunitId+"：不是重量单位，"+"不需要转换。");
            return BigDecimal.ONE;
        }
        Stockunit tounit = selectStockunitById(tounitId);
        if(tounit==null){
            throw new CustomException("物料单位获取异常"+tounitId);
        }
        if(tounit.getIsUnit()!=1){
            log.info(tounitId+"：不是重量单位，"+"不需要转换。");
            return BigDecimal.ONE;
        }
        return fromunit.getToT().divide(tounit.getToT(),6);
    }
}
